/**
 * nghilt
 * 2011/09/18
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanHuongXa extends QuanCo {

	public QuanHuongXa( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );	
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 2, 0 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nghi.test6.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		// Neu dung nguyen tai cho thi khong tinh la mot luot di chuyen
		if( toaDo == destination ) {
			return false;
		}
		
		// Neu khong cung mot cot
		if( !RectangleUtils.isInSameColumn( toaDo, destination ) ) {
			return false;
		}
		
//		Rectangle quanThap;
//		Rectangle quanCao;
//		
////		if( color == QUAN_DEN ) {
//			quanThap = destination;
//			quanCao = toaDo;
////		}else {
////			quanThap = toaDo;
////			quanCao = destination;
////		}
			
		Rectangle quanThap = toaDo;
		Rectangle quanCao = destination;
		
		if( normalDirection ) {
			if( color == QUAN_DEN ) {
				quanThap = destination;
				quanCao = toaDo;
			}
		}else {
			if( color == QUAN_TRANG ) {
				quanThap = destination;
				quanCao = toaDo;
			}
		}
		
		// Neu di lui
		if( RectangleUtils.convertToRealBoardRow( quanThap ) > RectangleUtils.convertToRealBoardRow( quanCao ) ) {
			return false;
		}
		
		// Neu bi can
		if ( biCan( destination, listQuanCo ) ) {
			return false;
		}
		
		// Dieu kien co ban khi di quan		
		return isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		QuanHuongXaPhongCap huongXaPhongCap = new QuanHuongXaPhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, huongXaPhongCap );	
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.board.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {
		
		// Neu khong vuot qua duoc dieu kien tha quan co ban
		if( !isPassToBasicReleaseCondition( board, destination, listQuanCo ) ) {
			return false;
		}		
		
		// Luat han che tha quan tot thu hai: khong duoc tha o hang cuoi cung
		if( board.isInVungCamThaQuanLoai1( destination, color ) ) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public boolean isTrongVungBatBuocPhongQuan( Board board, Rectangle destination ) {
		if( board.isInVungCamThaQuanLoai1( destination, color ) ) {
			return true;
		}
		return false;
	}
	
	public boolean biCan( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		
		int row = RectangleUtils.convertToRealBoardRow( toaDo );
		int nextRow = RectangleUtils.convertToRealBoardRow( destination );
		
		int max = Math.max( row, nextRow );
		int min = Math.min( row, nextRow );
		
		for ( int j = 0; j < 32; j++ ) {
			if( RectangleUtils.isInSameColumn( toaDo, listQuanCo.get( j ).toaDo ) 
					&& ( RectangleUtils.convertToRealBoardRow( listQuanCo.get( j ).toaDo ) > min 
							&& RectangleUtils.convertToRealBoardRow( listQuanCo.get( j ).toaDo ) < max ) ) {
				return true;
			}
		}		

		return false;
	}
}
